package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.poi.ExcelUtil;
import com.project.domain.bo.CooperateUnitBo;
import com.project.domain.bo.ProjectBudgetBo;
import com.project.domain.bo.ProjectTabListBo;
import com.project.domain.bo.ProjectInfoBo;
import com.project.domain.entity.*;
import com.project.domain.entity.xjProject.BizXjProject;
import com.project.domain.entity.xjProject.BizXjProjectDocument;
import com.project.domain.entity.xjProject.BizXjProjectMember;
import com.project.domain.entity.zxProject.BizZxProject;
import com.project.domain.entity.zxProject.BizZxProjectDocument;
import com.project.domain.entity.zxProject.BizZxProjectMember;
import com.project.domain.vo.ProListInfoVo;
import com.project.service.IProjectInitService;
import com.project.service.IYfContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "项目立项")
@RestController
@RequestMapping("/pro/init")
public class ProjectInitController extends BaseController {

    @Autowired
    private IProjectInitService projectInitService;

    @Autowired
    private IYfContractService yfContractService;

    @ApiOperation("列表左上方统计")
    @GetMapping("/groups")
    public AjaxResult groups(ProjectInfoBo projectInfoBo) {
        return success(projectInitService.selectGroups(projectInfoBo));
    }

    @ApiOperation("获取项目分布(左侧导航栏)")
    @GetMapping("/navigation")
    public AjaxResult getProNavigation() {
        return success(projectInitService.getProNavigation());
    }

    @ApiOperation("获取项目各页签上方信息")
    @GetMapping("/otherBase")
    public AjaxResult getProOtherInfo(@Param("proId") String proId) {
        return success(projectInitService.getProOtherInfo(proId,"all"));
    }

    @ApiOperation("获取项目列表")
    @PostMapping("/list")
    public TableDataInfo proList(@RequestBody ProjectInfoBo projectInfoBo) {
        IPage<ProListInfoVo> proList = projectInitService.getProList(projectInfoBo, "select");
        return TableDataInfo.build(proList.getRecords(), proList.getTotal());
    }

    //region 查询页签
    @ApiOperation("基础信息")
    @GetMapping("/base/{ID}")
    public AjaxResult baseinfo(@PathVariable("ID") String ID) {
        String proClass = projectInitService.getProClassByProId(ID);
        if (proClass.equals("xJProject")) {
            return success(projectInitService.xjDetail(ID));
        } else if (proClass.equals("zXProject")) {
            return success(projectInitService.zxDetail(ID));
        } else if(proClass.equals("yfContract")){
            return success(yfContractService.getYfBase(ID));
        }else{
            return error("主项目不存在！");
        }
//        return success(projectInitService.baseDetail(ID));
    }

    @ApiOperation("项目成员")
    @GetMapping("/member")
    public AjaxResult memberDetail(@Param("proId") String proId) {
        return success(projectInitService.memDetail(proId));
    }

    @ApiOperation("校内课题")
    @GetMapping("/keti/{proId}")
    public AjaxResult ketiDetail(@PathVariable("proId") String proId) {
        return success(projectInitService.zxKetiDetail(proId));
    }

    @ApiOperation("合作单位")
    @GetMapping("/cooperationUnit/{proId}")
    public AjaxResult zxCooperationUnitDetail(@PathVariable("proId") String proId) {
        return success(projectInitService.zxCooperationUnitDetail(proId));
    }

    @ApiOperation("项目预算")
    @GetMapping("/budget")
    public AjaxResult budgetDetail(@Param("proId") String proId) {
        return success(projectInitService.budgetDetail(proId));
    }

    @ApiOperation("项目经费")
    @GetMapping("/money/{proId}")
    public AjaxResult moneyDetail(@PathVariable("proId") String proId) {
        return success(projectInitService.moneyDetail(proId));
    }

    @ApiOperation("出账合同")
    @GetMapping("/buyer/{proId}")
    public AjaxResult buyerDetail(@PathVariable("proId") String proId) {
        return success(projectInitService.buyerDetail(proId));
    }

    @ApiOperation("项目文档")
    @GetMapping("/document/{proId}")
    public AjaxResult documentDetail(@PathVariable("proId") String proId) {
        return success(projectInitService.documentDetail(proId));
    }

    @ApiOperation("衍生成果")
    @GetMapping("/achievement/{proId}")
    public AjaxResult xjAchievementDetail(@PathVariable("proId") String proId) {
        return success(projectInitService.achievementDetail(proId));
    }

    @ApiOperation("执行过程")
    @GetMapping("/process/{proId}")
    public AjaxResult xjProcessDetail(@PathVariable("proId") String proId) {
        return success(projectInitService.processDetail(proId));
    }
    //endregion

    //region 保存（校级、纵向、横向）

    //region 校级保存(基础、成员、文件)
    @ApiOperation("保存校级项目基础信息：有proid时更新，没有时新建")
    @PostMapping("/saveXjBase")
    public AjaxResult saveXjInfos(@RequestBody BizXjProject bizXjProject) {
        return projectInitService.saveXjProInfos(bizXjProject);
    }

    @ApiOperation("保存校级项目成员信息")
    @PostMapping("/saveXjMember")
    public AjaxResult saveXjMember(@RequestBody ProjectTabListBo<BizXjProjectMember> xjProjectMemberList) {
        return projectInitService.saveXjMember(xjProjectMemberList);
    }

    @ApiOperation("保存校级项目文档信息")
    @PostMapping("/saveXjFile")
    public AjaxResult saveXjFile(@RequestBody ProjectTabListBo<BizXjProjectDocument> projectTabListBo) {
        return projectInitService.saveXjFile(projectTabListBo);
    }
    //endregion

    //region 纵向保存（基础、成员、课题、合作单位、文件）
    @ApiOperation("保存纵向项目基础信息：有proid时更新，没有时新建")
    @PostMapping("/saveZxBase")
    public AjaxResult saveZxInfos(@RequestBody BizZxProject bizZxProject) {
        return projectInitService.saveZxProInfos(bizZxProject,"normor");
    }

    @ApiOperation("保存纵向项目成员信息")
    @PostMapping("/saveZxMember")
    public AjaxResult saveZxMember(@RequestBody ProjectTabListBo<BizZxProjectMember> zxProjectMemberList) {
        return projectInitService.saveZxMember(zxProjectMemberList);
    }

    @ApiOperation("保存纵向项目校内课题")
    @PostMapping("/saveZxKeti")
    public AjaxResult saveKeti(@RequestBody ProjectTabListBo<BizProjectInSchool> projectTabListBo) {
        return projectInitService.saveKetiList(projectTabListBo);
    }

    @ApiOperation("保存纵向项目合作单位")
    @PostMapping("/saveZxUnit")
    public AjaxResult saveCooperUnit(@RequestBody CooperateUnitBo cooperateUnitBo) {
        return projectInitService.saveCooperUnit(cooperateUnitBo);
    }

    @ApiOperation("保存纵向项目文档信息")
    @PostMapping("/saveZxFile")
    public AjaxResult saveZxFile(@RequestBody ProjectTabListBo<BizZxProjectDocument> projectTabListBo) {
        return projectInitService.saveZxFile(projectTabListBo);
    }
    //endregion

    //region 横向保存（基础、成员、文档）
    //------------------------------横向项目保存 ↓------------------------------------
    @ApiOperation("保存横向进账合同")
    @PostMapping("/saveYfInfos")
    public AjaxResult saveYfInfos(@RequestBody BizYfContract yfContract) {
        String  yfContractId = yfContractService.saveYfInfos(yfContract);
        return AjaxResult.success("操作成功",yfContractId);
    }

    @ApiOperation("修改横向进账合同")
    @PostMapping("/editYfInfos")
    public AjaxResult editYfInfos(@RequestBody BizYfContract yfContract) {
        return yfContractService.editYfInfos(yfContract);
    }

    @ApiOperation("保存横向进账合同成员信息")
    @PostMapping("/saveYfMember")
    public AjaxResult saveYfMember(@RequestBody ProjectTabListBo<BizYfContractMember>  yfContractMemberList) {
        return yfContractService.saveYfMember(yfContractMemberList);
    }

    @ApiOperation("保存横向进账合同 合同文档")
    @PostMapping("/saveYfFile")
    public AjaxResult saveYfFile(@RequestBody ProjectTabListBo<BizYfContractDocument> projectTabListBo) throws Exception {
        return  yfContractService.saveYfFile(projectTabListBo);
    }


    //------------------------------横向项目保存 ↑------------------------------------
    //endregion

    @ApiOperation("保存项目预算信息")
    @PostMapping("/saveBudget")
    public AjaxResult saveBudget(@RequestBody ProjectBudgetBo projectBudgetBo) {
        return projectInitService.saveBudget(projectBudgetBo);
    }
    //endregion

    //region 导出

    /**
     * 导出项目立项列表
     */
    @Log(title = "项目立项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody ProjectInfoBo projectInfoBo) {
        IPage<ProListInfoVo> list = projectInitService.getProList(projectInfoBo, "export");
        ExcelUtil<ProListInfoVo> util = new ExcelUtil<ProListInfoVo>(ProListInfoVo.class);
        util.showColumn(projectInfoBo.getExportStr().split(","));
        String proClass = projectInfoBo.getProClass();
        String sheetName = proClass.equals("xJProject") ? "校级" : proClass.equals("zXProject") ? "纵向" : "横向";
        util.exportExcelByOrder(response, list.getRecords(), sheetName + "项目立项数据", projectInfoBo.getExportStr().split(","));
    }
    //endregion

    @ApiOperation("删除项目信息")
    @GetMapping("/delInfos")
    public AjaxResult delInfos(@Param("proId") String proId) {
        String res = projectInitService.delInfos(proId);
        if (res.contains("成功")) {
            return success(res);
        } else {
            return error(res);
        }
    }

    @ApiOperation("最后页签提交 + 审核通过")
    @GetMapping("/saveAndCheck")
    public AjaxResult saveAndCheck(@ApiParam(value = "项目号") @RequestParam(value = "proId") String proId,
                                   @ApiParam(value = "提交1，审核通过2") @RequestParam(value = "submitType") Integer submitType) {
        return projectInitService.saveAndCheck(proId,submitType,null);
    }

}
